package com.bluedot.bank.action;

import java.util.HashSet;
import java.util.Set;

import com.bluedot.bank.dao.HibernateDaoSupport;
import com.bluedot.bank.dao.privilege.Privilege;
import com.bluedot.bank.dao.privilege.PrivilegeDao;
import com.bluedot.bank.dao.privilege.PrivilegeDaoImpl;
import com.bluedot.bank.dao.role.Role;
import com.bluedot.bank.dao.role.RoleDao;
import com.bluedot.bank.dao.role.RoleDaoImpl;
import com.opensymphony.xwork2.ActionSupport;

public class ModifyRoleAction extends ActionSupport {
	private long[] chosenAuthorizationsId;
	private long id;
	private Role role = new Role();
	private Set<Privilege> allAuthrizations = new HashSet<Privilege>();
	private RoleDao roleDao = new RoleDaoImpl();
	private PrivilegeDao privilegeDao = new PrivilegeDaoImpl();
	{
		allAuthrizations.addAll(privilegeDao.getAll());
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public String findRole() {
		role = roleDao.findById(id);
		Set<Privilege> set = roleDao.findById(id).getPrivileges();
		chosenAuthorizations.addAll(set);
		return SUCCESS;
	}

	public Set<Privilege> getAllAuthrizations() {
		return allAuthrizations;
	}

	public void setAllAuthrizations(Set<Privilege> allAuthrizations) {
		this.allAuthrizations = allAuthrizations;
	}

	public Set<Privilege> getChosenAuthorizations() {
		return chosenAuthorizations;
	}

	public void setChosenAuthorizations(Set<Privilege> chosenAuthorizations) {
		this.chosenAuthorizations = chosenAuthorizations;
	}

	public String modify() {

		//role.getPrivileges().clear();
		if(chosenAuthorizationsId!=null && chosenAuthorizationsId.length>0)
		for (int i = 0; i < chosenAuthorizationsId.length; i++) {
			Privilege privilege = privilegeDao
					.findById(chosenAuthorizationsId[i]);
			chosenAuthorizations.add(privilege);
			
		}
		//role.getPrivileges().removeAll(role.getPrivileges());
		
		//role.setPrivileges(chosenAuthorizations);
		//role.getPrivileges().addAll(chosenAuthorizations);
		long idr=role.getId();
		Role role1=this.roleDao.findById(idr);
		role1.setPrivileges(chosenAuthorizations);
		role1.setRoleName(role.getRoleName());
		role1.setDescription(role.getDescription());
		roleDao.update(role1);
		return SUCCESS;
	}

	private Set<Privilege> chosenAuthorizations = new HashSet<Privilege>(); /*
																			 * {
																			 */

	/**
	 * 
	 */
	/*
	 * private static final long serialVersionUID = 1L;
	 *  { Set<Privilege> set=roleDao.findById(id).getPrivileges();
	 * this.addAll(set); } };
	 */
	public Set<Privilege> getRemainAuthorizations() {
		Set<Privilege> returnValue = new HashSet<Privilege>(
				this.allAuthrizations);
		returnValue.removeAll(this.chosenAuthorizations);
		return returnValue;
	}

	public long[] getChosenAuthorizationsId() {
		return chosenAuthorizationsId;
	}

	public void setChosenAuthorizationsId(long[] chosenAuthorizationsId) {
		this.chosenAuthorizationsId = chosenAuthorizationsId;
	}
}
